package com.sls.game.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.GoodsInfoManageDAO;
import com.sls.game.dao.SupplyRuleDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.GoodsManageInfo;
import com.sls.game.pojo.SupplyRuleInfo;
import com.sls.game.querybean.SupplyRuleQuery;
import com.sls.game.service.SupplyRuleService;

@Service("supplyRuleService")
public class SupplyRuleServiceImpl implements SupplyRuleService {
	private static Logger log = Logger.getLogger(SupplyRuleServiceImpl.class);
	@Resource(name = "supplyRuleDAO")
	private SupplyRuleDAO supplyRuleDAO;
	@Resource(name = "goodsInfoManageDAO")
	private GoodsInfoManageDAO goodsInfoManageDAO;

	private Gson gson;

	public SupplyRuleServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public String save(String body, Head head) throws ServerDisponseException {
		SupplyRuleInfo ruleInfo = gson.fromJson(body, SupplyRuleInfo.class);
		this.validate(ruleInfo);
		this.validateFieldForInsert(ruleInfo);
		ruleInfo.setInsertTime(ServerDateAndTime.getNowDateTimeOfFormat());
		GoodsManageInfo goods = goodsInfoManageDAO
				.queryGoodsInfoByGoodsId(ruleInfo.getGoodsId());
		if (goods == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_NOT_EXIST.getCode(),
					ExceptionCodeDes.GOODS_NOT_EXIST.getDescription());
		}
		SupplyRuleInfo ruleInfo2 = supplyRuleDAO.queryRecordById(ruleInfo
				.getId());
		if (ruleInfo2 != null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SUPPLY_RULE_EXIST.getCode(),
					ExceptionCodeDes.SUPPLY_RULE_EXIST.getDescription());
		}
		ruleInfo.setId(UUID.randomUUID().toString());
		ruleInfo.setGoodsName(goods.getGoodsName());

		try {
			this.supplyRuleDAO.insert(ruleInfo);
			return null;
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public String update(String body, Head head) throws ServerDisponseException {
		SupplyRuleInfo ruleInfo = gson.fromJson(body, SupplyRuleInfo.class);
		this.validate(ruleInfo);
		this.validateField(ruleInfo);
		ruleInfo.setModifyTime(ServerDateAndTime.getNowDateTimeOfFormat());
		try {
			this.supplyRuleDAO.update(ruleInfo);
			return null;
		} catch (Exception e) {
			log.error(e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public String queryRuleByGoodsId(String body, Head head)
			throws ServerDisponseException {
		SupplyRuleInfo ruleInfo = gson.fromJson(body, SupplyRuleInfo.class);
		this.validate(ruleInfo);
		this.validateField(ruleInfo);
		String id = ruleInfo.getId();
		try {
			ruleInfo = this.supplyRuleDAO.queryRecordById(id);
			if (ruleInfo != null) {
				return gson.toJson(ruleInfo);
			}
			return null;
		} catch (Exception e) {
			log.error(e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public String querySupplyRule(String body, Head head)
			throws ServerDisponseException {
		SupplyRuleQuery ruleQuery = gson.fromJson(body, SupplyRuleQuery.class);
		this.validate(ruleQuery);
		try {
			Pagination<SupplyRuleInfo> page = this.supplyRuleDAO
					.querySupplyRule(ruleQuery);
			if (page != null) {
				return gson.toJson(page);
			}
			return null;
		} catch (Exception e) {
			log.error(e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	/**
	 * 校验是否为null
	 * 
	 * @param priceInfo
	 */
	private void validate(Object obj) throws ServerDisponseException {
		if (obj == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getDescription());
		}
	}

	/**
	 * 校验属性是否为空
	 * 
	 * @param ruleInfo
	 * @throws ServerDisponseException
	 */
	private void validateField(SupplyRuleInfo ruleInfo)
			throws ServerDisponseException {
		if (StringUtils.isEmpty(ruleInfo.getId())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ID_VALUE_NULL.getCode(),
					ExceptionCodeDes.ID_VALUE_NULL.getDescription());
		}
		if (StringUtils.isEmpty(ruleInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
	}
	private void validateFieldForInsert(SupplyRuleInfo ruleInfo)
			throws ServerDisponseException {
		if (StringUtils.isEmpty(ruleInfo.getGoodsId())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_ID_EMPTY.getCode(),
					ExceptionCodeDes.GOODS_ID_EMPTY.getDescription());
		}
		if (StringUtils.isEmpty(ruleInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
	}
}
